package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**角色管理服务实例
 * @author YH
 * @date 2020/4/16 14:31
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private PermissionDao permissionDao;

    //分页查询角色列表
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询角色列表
        String queryString = queryPageBean.getQueryString();
        List<Role> roles = roleDao.findPage(queryString);
        PageInfo info = new PageInfo(roles);
        return new PageResult(info.getTotal(),info.getList());
    }


    //根据角色id查询关联的菜单和权限
    @Override
    public List<Map<String, Object>> findRoleIdByPermission(Integer roleId) {
        List<Map<String,Object>> list = new ArrayList<>();
        List<Map<String,Object>> listPermission = new ArrayList<>();

        //查询角色关联的菜单
        list = menuDao.findRoleIdByPermission(roleId);
        //查询角色关联的权限
        listPermission = permissionDao.findRoleIdByPermission(roleId);
        for (Map<String, Object> map : listPermission) {
            list.add(map);
        }
        return list;
    }

    //根据角色id删除角色
    @Override
    public void findDeleteByIdRole(Integer roleId) {
        //如果角色关联菜单，不允许删除
        Integer menuCount = menuDao.findRoleIdByCountMenu(roleId);
        if (menuCount > 0){
            throw new RuntimeException(MessageConstant.DELETE_ROLE_MENU_FAIL);
        }
        //如果角色关联权限，不允许删除
        Integer permissionCount = permissionDao.findRoleIdByCountPermission(roleId);
        if (permissionCount > 0){
            throw new RuntimeException(MessageConstant.DELETE_ROLE_PERMISSION_FAIL);
        }
        //如果角色关联用户，不允许删除
        Integer userCount = userDao.findRoleIdByCountUser(roleId);
        if (userCount > 0){
            throw new RuntimeException(MessageConstant.DELETE_ROLE_USER_FAIL);
        }
        //删除角色
        roleDao.findRoleIdDeleteRole(roleId);
    }

    //新增角色
    @Override
    public void add(Integer[] menuIds, Integer[] permissionIds, Role role) {
        //添加
        roleDao.add(role);
        //添加角色与菜单的关系
        if (menuIds.length > 0){
            setMenuAndRole(role.getId(),menuIds);
        }

        if (permissionIds.length > 0){
            setPermissionAndRole(role.getId(),permissionIds);
        }
    }

    //查询所有角色信息
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    //根据角色id获取角色信息
    @Override
    public Role findById(Integer roleId) {
        return roleDao.findById(roleId);
    }

    //根据角色id查询角色关联的菜单id
    @Override
    public List<Integer> findRoleByMenu(Integer roleId) {
        return roleDao.findRoleByMenu(roleId);
    }

    //根据角色id查询角色关联的权限id
    @Override
    public List<Integer> findRoleByPermission(Integer roleId) {
        return roleDao.findRoleByPermission(roleId);
    }

    //编辑角色信息
    @Override
    public void edit(Integer[] menuIds, Integer[] permissionIds, Role role) {
        //删除角色关联的菜单信息
        roleDao.deleteRoleByMenu(role.getId());
        //删除角色关联的权限信息
        roleDao.deleteRoleByPermission(role.getId());
        //修改角色信息
        roleDao.editRole(role);
        //重新添加角色关联的菜单信息
        if (menuIds.length > 0){
            setMenuAndRole(role.getId(),menuIds);
        }
        //重新添加角色关联的权限信息
        if (permissionIds.length > 0){
            setPermissionAndRole(role.getId(),permissionIds);
        }

    }

    //查询关键字是否唯一
    @Override
    public Integer findRoleKeyWordBycount(String keyword) {
        return roleDao.findRoleKeyWordBycount(keyword);
    }

    //添加角色与权限的关系
    private void setPermissionAndRole(Integer id, Integer[] permissionIds) {
        Map<String,Object> map = new HashMap<>();
        for (Integer permissionId : permissionIds) {
            map.put("roleId",id);
            map.put("permissionId",permissionId);
            roleDao.setPermissionAndRole(map);
        }
    }

    //添加角色与菜单的关系
    private void setMenuAndRole(Integer id, Integer[] menuIds) {
        Map<String,Object> map = new HashMap<>();
        for (Integer menuId : menuIds) {
            map.put("roleId",id);
            map.put("menuId",menuId);
            roleDao.setMenuAndRole(map);
        }
    }




}
